﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FormDimEquipment : System.Windows.Forms.Form
	{
		public FormDimEquipment(bool bIsaPlan)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{B3E466A7-E6DD-49A3-8A3D-EBF0F80F347E}", null);
			base.Icon = Resource.YArchitechIcon;
			this.checkBox1.Enabled = bIsaPlan;
			UserSetting userSetting = new UserSetting();
			this.dimEquipmentData = userSetting.GetDimEquipmentSetting();
			this.InitDlg();
			this.UpdataPng();
		}

		private void InitDlg()
		{
			DimEquipmentData dimEquipmentData = new DimEquipmentData(this.dimEquipmentData);
			if (dimEquipmentData.DimStype == 1)
			{
				this.radioFG.Checked = true;
			}
			else
			{
				this.radioOther.Checked = true;
			}
			this.txtFloorName.Text = dimEquipmentData.FloorName;
			this.txtDimData.Text = dimEquipmentData.DimContent;
			if (dimEquipmentData.NumberType == 1)
			{
				this.radioButton1.Checked = true;
			}
			else
			{
				this.radioButton2.Checked = true;
			}
			this.txtNumber.Text = dimEquipmentData.NumberContent;
			this.checkBox1.Checked = dimEquipmentData.IsLead;
			this.CheckEnable();
		}

		private void CheckEnable()
		{
			bool @checked = this.radioFG.Checked;
			this.label1.Visible = @checked;
			this.txtFloorName.Visible = @checked;
			this.radioButton1.Visible = @checked;
			this.radioButton2.Visible = @checked;
			this.txtNumber.Visible = @checked;
			this.label2.Visible = !@checked;
			this.txtDimData.Visible = !@checked;
		}

		private void UpdataFormData()
		{
			if (this.radioFG.Checked)
			{
				this.dimEquipmentData.DimStype = 1;
			}
			else
			{
				this.dimEquipmentData.DimStype = 2;
			}
			this.dimEquipmentData.FloorName = this.txtFloorName.Text;
			this.dimEquipmentData.DimContent = this.txtDimData.Text;
			if (this.radioButton1.Checked)
			{
				this.dimEquipmentData.NumberType = 1;
			}
			else
			{
				this.dimEquipmentData.NumberType = 2;
			}
			this.dimEquipmentData.NumberContent = this.txtNumber.Text;
			this.dimEquipmentData.IsLead = this.checkBox1.Checked;
		}

		private void ContentChange(object sender, EventArgs e)
		{
			this.UpdataFormData();
		}

		private void FormDimEquipment_FormClosed(object sender, FormClosedEventArgs e)
		{
			new UserSetting().WriteDimEquipmentSetting(this.dimEquipmentData);
			this.SimulationPressESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormDimEquipment.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormDimEquipment.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void radioFG_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdataFormData();
			this.CheckEnable();
			this.UpdataPng();
		}

		private void radioButton1_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdataFormData();
			this.txtNumber.Enabled = this.radioButton2.Checked;
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdataFormData();
			this.UpdataPng();
		}

		private void UpdataPng()
		{
			if (this.dimEquipmentData.DimStype != 1)
			{
				Bitmap bitmap = YArchitech.HVAC.Properties.Resources.DimEquipment01;
				if (bitmap != null)
				{
					bitmap = new Bitmap(bitmap, new Size(180, 120));
				}
				this.pictureBox1.Image = bitmap;
				return;
			}
			if (this.dimEquipmentData.IsLead)
			{
				Bitmap bitmap2 = YArchitech.HVAC.Properties.Resources.DimEquipmentLink;
				if (bitmap2 != null)
				{
					bitmap2 = new Bitmap(bitmap2, new Size(180, 120));
				}
				this.pictureBox1.Image = bitmap2;
				return;
			}
			Bitmap bitmap3 = YArchitech.HVAC.Properties.Resources.DimEquipment;
			if (bitmap3 != null)
			{
				bitmap3 = new Bitmap(bitmap3, new Size(180, 120));
			}
			this.pictureBox1.Image = bitmap3;
		}

		private void FormDimEquipment_Load(object sender, EventArgs e)
		{
			new YJKProcessTools().ShowInTaskbarChange(this);
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.KeyPreview = true;
		}

		private void FormDimEquipment_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				new UserSetting().WriteDimEquipmentSetting(this.dimEquipmentData);
				base.Close();
			}
		}

		private void FormDimEquipment_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		public DimEquipmentData dimEquipmentData;

		private ControlInfoManager m_thisManger;
	}
}
